package cn.china.springboot.controller;

import cn.china.springboot.serviceimpl.UserMenuServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
@CrossOrigin
@RestController
public class UserMenuController {
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    ListOperations<String, Object> listOperations;

    @RequestMapping(value = "/addUserMenu", produces = "application/json;charset:UTF-8")
    public List<Object> addUserMenu(@RequestParam Integer id, @RequestParam String url, @RequestParam String name, @RequestParam Double price, @RequestParam String username, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");

        List<Object> list = listOperations.range(username, 0, listOperations.size(username) - 1);

        list.add(id);
        list.add(url);
        list.add(name);
        list.add(price);
        listOperations.getOperations().delete(username);
        listOperations.leftPushAll(username, list);

        return list;
    }
   /* @RequestMapping(value = "/getUserMenu", produces = "application/json;charset:UTF-8")
    public List<Object> getUserMenu(@RequestParam String username, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*");
        System.out.println(listOperations.range(username, 0, listOperations.size(username) - 1));
        return listOperations.range(username, 0, listOperations.size(username) - 1);
    }*/
}

